Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.getFromAgent = exports.fillAgent = exports.storageAgent = exports.applyAgent = exports.loadAgent = exports.formatAgent = void 0;

var _api = _interopRequireDefault(require("./api")), _storage = _interopRequireDefault(require("./storage")), _util = _interopRequireDefault(require("../public/utils/util"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _slicedToArray(e, t) {
    return _arrayWithHoles(e) || _iterableToArrayLimit(e, t) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(e, t) {
    var r = [], n = !0, a = !1, o = void 0;
    try {
        for (var i, g = e[Symbol.iterator](); !(n = (i = g.next()).done) && (r.push(i.value), 
        !t || r.length !== t); n = !0) ;
    } catch (e) {
        a = !0, o = e;
    } finally {
        try {
            n || null == g.return || g.return();
        } finally {
            if (a) throw o;
        }
    }
    return r;
}

function _arrayWithHoles(e) {
    if (Array.isArray(e)) return e;
}

var app = getApp(), formatAgent = function(t) {
    return [ "today", "week", "golds", "totalgolds", "stroagegolds" ].forEach(function(e) {
        t["formmated" + _util.default.ucFirst(e)] = (t[e] / 100).toFixed(2);
    }), t.usertask = t.usertask.map(function(e) {
        return e.isOpen = !1, e.task.reverse(), e.task = e.task.map(function(e, t) {
            return e.index = t, e;
        }), e.singleTask = e.task.filter(function(e) {
            return e.isfinish;
        })[0], e;
    }), t;
};

exports.formatAgent = formatAgent;

var loadAgent = function(e) {
    var r = _slicedToArray(getCurrentPages(), 1)[0];
    return _api.default.minicard.spread.view({
        complete: e
    }).then(function(e) {
        var t = formatAgent(e.data);
        return app.globalData.agent = t, r.$emit("agent.change", t), t;
    });
};

exports.loadAgent = loadAgent;

var applyAgent = function() {
    return _api.default.minicard.spread.apply({
        loading: "申请中..."
    }).then(function() {
        return loadAgent();
    });
};

exports.applyAgent = applyAgent;

var STORAGE_MOBILE_TIME = 432e6, isShare = function(e) {
    return "share" === e || "scan" === e;
}, agentMobile = function(e) {
    _storage.default.set("agent", {
        mobile: e.mobile,
        time: Date.now()
    });
}, agentAgentId = function(e) {
    var t = e.agent, r = (t = void 0 === t ? {} : t).agentid;
    +r && _storage.default.set("fromagentid", {
        agentid: r,
        time: Date.now()
    });
}, storageAgent = function(e) {
    var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {};
    isShare(e) && (_storage.default.isExpire("agent", STORAGE_MOBILE_TIME) && agentMobile(t), 
    _storage.default.isExpire("fromagentid", STORAGE_MOBILE_TIME) && agentAgentId(t));
};

exports.storageAgent = storageAgent;

var fillAgent = function(e) {
    var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {};
    isShare(e) && (agentMobile(t), agentAgentId(t));
};

exports.fillAgent = fillAgent;

var getFromAgent = function() {
    var e = {
        fromagentid: "",
        frommobile: ""
    };
    return _storage.default.isExpire("agent", STORAGE_MOBILE_TIME) || (e.frommobile = _storage.default.getSync("agent").mobile), 
    _storage.default.isExpire("fromagentid", STORAGE_MOBILE_TIME) || (e.fromagentid = _storage.default.getSync("fromagentid").agentid), 
    e;
};

exports.getFromAgent = getFromAgent;